package com.xinziruo.crawl.domain.crawl.expression;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.xinziruo.crawl.domain.util.AbstractDomain;

/**
 * 
 * @author loudyn
 * 
 */
public class CrawlParseExpression extends AbstractDomain {

	private static final long serialVersionUID = 1L;

	private String name;
	private String descr;
	private String classify;
	private String project;
	private String expression;
	private String resultType;

	@SuppressWarnings("unchecked")
	private List<CrawlParseStringFilterConfig> stringFilterConfigs = LazyList.decorate(new ArrayList<CrawlParseStringFilterConfig>(),
			FactoryUtils.instantiateFactory(CrawlParseStringFilterConfig.class));

	
	public String getProject() {
		return project;
	}

	public void setProject(String project) {
		this.project = project;
	}
	
	public String getClassify() {
		return classify;
	}

	public CrawlParseExpression setClassify(String classify) {
		this.classify = classify;
		return this; 
	}

	public String getName() {
		return name;
	}

	public CrawlParseExpression setName(String name) {
		this.name = name;
		return this;
	}

	public String getDescr() {
		return descr;
	}

	public CrawlParseExpression setDescr(String descr) {
		this.descr = descr;
		return this;
	}

	public String getExpression() {
		return expression;
	}

	public CrawlParseExpression setExpression(String expression) {
		this.expression = expression;
		return this;
	}

	public String getResultType() {
		return resultType;
	}

	public CrawlParseExpression setResultType(String resultType) {
		this.resultType = resultType;
		return this;
	}

	public List<CrawlParseStringFilterConfig> getStringFilterConfigs() {
		return stringFilterConfigs;
	}

	public void setStringFilterConfigs(List<CrawlParseStringFilterConfig> crawlParseStringFilters) {
		this.stringFilterConfigs = crawlParseStringFilters;
	}

	public CrawlParseExpression addStringFilterConfig(CrawlParseStringFilterConfig filter) {
		getStringFilterConfigs().add(filter);
		return this;
	}

	public CrawlParseExpression verifyStringFilterConfigs() {
		
		for (CrawlParseStringFilterConfig filter : getStringFilterConfigs()) {
			if (StringUtils.isBlank(filter.getId())) {
				filter.setId(null);
			}
			
			filter.setCrawlParseExpression(this);
		}
		return this;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getId())
									.append(this.getName())
									.append(this.getDescr())
									.append(this.getExpression())
									.append(this.getClassify()) 
									.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (obj == this) {
			return true;
		}

		if (obj.getClass() != getClass()) {
			return false;
		}

		CrawlParseExpression other = (CrawlParseExpression) obj;
		return new EqualsBuilder().append(this.getId(), other.getId())
									.append(this.getName(), other.getName())
									.append(this.getDescr(), other.getDescr())
									.append(this.getExpression(), other.getExpression())
									.append(this.getClassify(),other.getClassify()) 
									.isEquals();
	}

	
}
